package com.handy.chen.jiyu;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;




import com.handy.chen.jiyu.image.ScoreView;
import com.kaloer.filepicker.FilePickerActivity;

public class SettingWallpaper extends Activity implements View.OnClickListener {
	
	
	private String TAG = "SettingWallpaper";
	private ScoreView scoreview;
	private Bitmap wallpaper;
	/*private Bitmap wallpaper;*/

	private Button btLib;
	private Button btSet;
	private Button btFromPhone; 
	private Button btPrev;
	private Button btNext;
	
	private static WallpaperManager myWallpaperManager;
	
	private String EXTRA_FILE_PATH = "file_path";
	private int image_resource_id_love_start = 0x7f020003;
	private int image_resource_id_love_end = 0x7f02001a;
	private int current_image_id = 0x7f020003;
	
	
	// camera 
	private Uri mImageUri;
	private Bitmap cur_img;
	private static final int SELECT_PICTURE = 1;
	private static final int TAKE_PICTURE = 2;
	private static final int SETTING_RETURN = 3;
	private String selectedImagePath;
    private String filemanagerstring;
	private String camera_temp_picture = Environment.getExternalStorageDirectory() + "/temp_pic.jpg";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setwallpaper);		
		
		init();
		
	
		
	}
	
	public void init(){
		scoreview = (ScoreView) findViewById(R.id.scoreview);
		scoreview.setOnClickListener(this);
		btFromPhone = (Button) findViewById(R.id.from_phone);
		btFromPhone.setOnClickListener(this);
		btLib = (Button) findViewById(R.id.library);
		btLib.setOnClickListener(this);
		btSet = (Button) findViewById(R.id.set_wallpaper);
		btSet.setOnClickListener(this);
		btPrev = (Button) findViewById(R.id.previous);
		btPrev.setOnClickListener(this);
		btNext = (Button) findViewById(R.id.next);
		btNext.setOnClickListener(this);
		
		
		myWallpaperManager
	     = WallpaperManager.getInstance(this);
		
		
		
		// chose the hand jpeg as the default picture
		current_image_id = image_resource_id_love_start;
		System.out.println(current_image_id);
		
		scoreview.initSet(image_resource_id_love_start);
		wallpaper = scoreview.wp_display;
		
		
				
	}
	

	
	public void onActivityResult(int requestCode, int resultCode, Intent data){
		
	    // use the result to check if the bluetooth is enabled 
		switch (resultCode){
		// get the file path
		case RESULT_OK:
			if (TAKE_PICTURE == requestCode){
				 
				Toast.makeText(this, "Good shot!", Toast.LENGTH_SHORT).show();
				
				scoreview.wp_display = BitmapFactory.decodeFile(camera_temp_picture);
				scoreview.initSet();
				wallpaper = scoreview.wp_display;
				
				
			}else{
				String file_path = data.getStringExtra(EXTRA_FILE_PATH);
	         	Log.d(TAG,file_path);
	         	
	         	scoreview.wp_display = BitmapFactory.decodeFile(file_path);
	    		scoreview.initSet();	
	    		wallpaper = scoreview.wp_display;
			
			}
			
		}
		
	}


	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		
		// open built in files
		case R.id.set_wallpaper:
			
			setting_up_wallpaper(wallpaper,this);
			break;
			
		// select the wallpaper;
			
		case R.id.library:
		
			// take picture 
			takePhoto();
			break;
			
		// use filepicker	
		case R.id.from_phone:
			// start file picker; 
			Intent intent0 = new Intent(this,FilePickerActivity.class);
    		startActivityForResult(intent0, 0);
			break;
			
			
		case R.id.next:
			// add one to the num 
			current_image_id++;
			if (current_image_id>image_resource_id_love_end){
				current_image_id = current_image_id - (image_resource_id_love_end-image_resource_id_love_start);
			}
			scoreview.wp_display = BitmapFactory.decodeResource(getResources(), current_image_id);
			scoreview.initSet();
			wallpaper = scoreview.wp_display;
			
			break;
		 
		case R.id.previous:
			// subtract one 
			current_image_id--;
			if (current_image_id < image_resource_id_love_start){
				current_image_id = current_image_id + (image_resource_id_love_end-image_resource_id_love_start);
			}
			
			scoreview.wp_display = BitmapFactory.decodeResource(getResources(), current_image_id);
			
			scoreview.initSet();
			wallpaper = scoreview.wp_display;
			
			break;	
			
		}
	}
	
	/**
	 *  set up the wall paper
	 * @param bmp
	 * @param context
	 */
	
	public static void setting_up_wallpaper(Bitmap bmp, Context context){
		
		
        Log.d("Setting Wallpaper","after yes");
        
        try {
        	myWallpaperManager.setBitmap(bmp);
			Toast.makeText(context, "success", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			
		}
	}
	
	
	public void setting_up_wallpape(int Resource_id){
		
		myWallpaperManager
	     = WallpaperManager.getInstance(this);
      
       Log.d("Setting Wallpaper","after yes");
       
       
       Bitmap bmp = BitmapFactory.decodeResource(getResources(), Resource_id);
       	   
       try {
       		myWallpaperManager.setBitmap(bmp);
			Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			
		}
	}
	
	
	private boolean takePhoto(){

    	PackageManager pm = this.getPackageManager();

    	if (!(pm.hasSystemFeature(PackageManager.FEATURE_CAMERA))) {
    		Toast.makeText(this, "Can't find camera", Toast.LENGTH_SHORT).show(); 
    		return false;
    	}


    	Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    	//Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    	String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    	path += "/temp_pic.jpg";
    	File file = new File( path );
    	//file.mkdirs();
    	mImageUri = Uri.fromFile( file );
    	//String absoluteOutputFileUri = file.getAbsolutePath();

    	intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
    	startActivityForResult(intent, TAKE_PICTURE);
    	

	    return true; 
    }
	
	
	
	
	
}
